import "package:ct_pa_majordomo/utils/request.dart";
import "package:dio/dio.dart";

class LockApi {
  Future<Map> initLock(queryParameters) async {
    Response response = await request.post(
      "device/lock",
      data: queryParameters,
    );
    return response.data ?? {};
  }

  Future<Map> getLockList(queryParameters) async {
    Response response = await request.get(
      "device/lock/list",
      queryParameters: queryParameters,
    );
    return response.data ?? {};
  }

  Future<Map> getLockInfo(lockId) async {
    Response response = await request.get(
      "device/lock/$lockId",
    );
    return response.data ?? {};
  }

  Future<Map> bindRoom(queryParameters) async {
    Response response = await request.put(
      "device/lock/bind-room",
      data: queryParameters,
    );
    return response.data ?? {};
  }

  Future<Map> unbindRoom(int lockId) async {
    Response response = await request.post(
      "device/lock/unbind/$lockId",
    );
    return response.data ?? {};
  }

  ///未关联房间锁列表
  Future<Map> unbindList() async {
    Response response = await request.get(
      "device/lock/unbind-list",
    );
    return response.data ?? {};
  }

  Future<Map> removeLock(lockIds) async {
    Response response = await request.delete(
      "device/lock/$lockIds",
    );
    return response.data ?? {};
  }

  Future<Map> editLock(data) async {
    Response response = await request.put(
      "device/lock",
      data: data
    );
    return response.data ?? {};
  }

  Future<Map> unLock(int lockId) async {
    Response response = await request.post(
        "device/lock/unlock/$lockId",
    );
    return response.data ?? {};
  }

  Future<Map> syncLock(int lockId) async {
    Response response = await request.post(
      "device/lock/sync-lock/$lockId",
    );
    return response.data ?? {};
  }


  /// 修改门锁管理员密码
  /// data: {
  ///   "adminPasscode": "123456",
  ///   "changeType": 1, 1:上传 ，2：修改
  ///   "lockId": 1,
  /// }
  Future<Map> updateAdminPwd(int lockId, data) async {
    Response response = await request.put(
      "device/lock/admin-passcode/$lockId",
      queryParameters: data,
    );
    return response.data ?? {};
  }

  /// 电子钥匙相关
  /// 发送钥匙
  Future<Map> sendKey(data) async {
    Response response = await request.post(
      "device/lock/send-key",
      data: data,
    );
    return response.data ?? {};
  }

  /// 批量发送钥匙
  Future<Map> sendKeys(data) async {
    Response response = await request.post(
      "device/lock/send-keys",
      data: data,
    );
    return response.data ?? {};
  }

  /// 钥匙列表
  Future<Map> getKeyList(int lockId, queryParameters) async {
    Response response = await request.get(
      "device/lock/keys/$lockId",
      queryParameters: queryParameters
    );
    return response.data ?? {};
  }

  /// 删除钥匙
  Future<Map> deleteKey(int lockId, int keyId) async {
    Response response = await request.delete(
      "device/lock/key/$lockId/$keyId",
    );
    return response.data ?? {};
  }
  /// 重置钥匙
  Future<Map> resetKey(int lockId) async {
    Response response = await request.delete(
      "device/lock/key/clear/$lockId",
    );
    return response.data ?? {};
  }



  ///更新锁数据
  Future<Map> updateLockData(data) async {
    Response response = await request.post(
      "device/lock/update-lock-data",
      queryParameters: data
    );
    return response.data ?? {};
  }

  ///更新电量
  Future<Map> updateQuantity(data) async {
    Response response = await request.post(
        "device/lock/update-quantity/${data["lockId"]}",
        data: data
    );
    return response.data ?? {};
  }

  /// 房间关联门锁列表
  Future<Map> lockByRoomList(int roomId) async {
    Response response = await request.get(
      "device/lock/listByRoomIds",
      queryParameters: { "roomIds": roomId},
    );
    return response.data ?? {};
  }

  /// 获取门锁开锁记录
  Future<Map> getUnlockRecords(int lockId, queryParameters) async {
    Response response = await request.get(
      "device/lock/records/$lockId",
      queryParameters: queryParameters,
    );
    return response.data ?? {};
  }

  /// 手动读取门锁记录
  Future<Map> uploadUnlockRecords(int lockId, String records) async {
    Response response = await request.post(
      "device/lock/upload-record/$lockId",
      queryParameters: {"records": records},
    );
    return response.data ?? {};
  }

  /// 指纹相关
  /// 获取指纹列表
  Future<Map> getFingerprints(int lockId, queryParameters) async {
    Response response = await request.get(
      "device/lock/fingerprints/$lockId",
      queryParameters: queryParameters,
    );
    return response.data ?? {};
  }

  /// 上传指纹
  Future<Map> uploadFingerprint(data) async {
    Response response = await request.post(
      "device/lock/upload-fingerprint",
      data: data,
    );
    return response.data ?? {};
  }

  /// 删除指纹
  Future<Map> deleteFingerprint(int lockId, int fingerprintId) async {
    Response response = await request.delete(
      "device/lock/fingerprint/$lockId/$fingerprintId",
    );
    return response.data ?? {};
  }

  /// 重置指纹
  Future<Map> resetFingerprint(int lockId) async {
    Response response = await request.delete(
      "device/lock/fingerprint/clear/$lockId",
    );
    return response.data ?? {};
  }

  /// 添加IC卡
  Future<Map> addICCard(Map<String, dynamic> data) async {
    Response response = await request.post(
      "device/lock/ic-card",
      data: data,
    );
    return response.data ?? {};
  }

  /// 更新wifi锁网络信息
  Future<Map> updateNetwork(data) async {
    Response response = await request.post(
      "device/lock/wifi/update-network/${data["lockId"]}",
      queryParameters: data,
    );
    return response.data ?? {};
  }

  /// 获取wifi锁详情
  Future<Map> getWifiLockInfo(int lockId) async {
    Response response = await request.get(
      "device/lock/wifi/detail/$lockId",
    );
    return response.data ?? {};
  }

  /// 门锁密码相关
  /// 添加密码
  Future<Map> sendPwd(data) async {
    Response response = await request.post(
      "device/lock/add-pwd",
      data: data,
    );
    return response.data ?? {};
  }

  /// 批量添加密码
  Future<Map> sendPwds(data) async {
    Response response = await request.post(
      "device/lock/add-pwds",
      data: data,
    );
    return response.data ?? {};
  }

  /// 获取随机密码
  Future<Map> getRandomPwd(int lockId, params) async {
    Response response = await request.post(
      "device/lock/get-random-pwd/$lockId",
      queryParameters: params,
    );
    return response.data ?? {};
  }

  /// 密码列表
  Future<Map> getPwdList(int lockId, queryParameters) async {
    Response response = await request.get(
      "device/lock/pwdList/$lockId",
      queryParameters: queryParameters,
    );
    return response.data ?? {};
  }

  /// 删除密码
  Future<Map> deletePassword(int lockId, int passwordId) async {
    Response response = await request.delete(
      "device/lock/password/$lockId/$passwordId",
    );
    return response.data ?? {};
  }

  /// 批量删除密码
  Future<Map> clearPasswords(int lockId) async {
    Response response = await request.delete(
      "device/lock/password/clear/$lockId",
    );
    return response.data ?? {};
  }

  /// 获取门锁关联的网关列表
  Future<Map> getGatewayList(int lockId) async {
    Response response = await request.get(
      "device/lock/gateway-list/$lockId",
    );
    return response.data ?? {};
  }

  /// ic卡相关
  /// 获取ic卡列表
  Future<Map> getIcCardList(int lockId, queryParameters) async {
    Response response = await request.get(
      "device/lock/cards/$lockId",
      queryParameters: queryParameters,
    );
    return response.data ?? {};
  }
  /// 获取ic卡列表
  Future<Map> addCard(int lockId, data) async {
    Response response = await request.post(
      "device/lock/add-card",
      data: data,
    );
    return response.data ?? {};
  }
  /// 删除卡号
  Future<Map> delCard(int lockId, int cardId) async {
    Response response = await request.delete(
      "device/lock/card/$lockId/$cardId",
    );
    return response.data ?? {};
  }
  /// 清空卡号
  Future<Map> clearCard(int lockId) async {
    Response response = await request.delete(
      "device/lock/card/clear/$lockId",
    );
    return response.data ?? {};
  }
  /// 修改IC卡有效期
  Future<Map> periodCard(int lockId, int cardId, data) async {
    Response response = await request.put(
      "device/lock/card-period/$lockId/$cardId",
      data: data
    );
    return response.data ?? {};
  }

  /// 门锁设置
  /// 自动闭锁
  /// data: {
  ///   "seconds": 10,
  ///   "lockId": 1,
  ///   "changeType": 1, 1:上传 ，2：网关修改
  /// }
  Future<Map> setAutoLock(int lockId, data) async {
    Response response = await request.post(
      "device/lock/autoLockTime/$lockId",
      queryParameters: data,
    );
    return response.data ?? {};
  }

  /// 修改门锁设置项
  /// data: {
  ///   lockId: 2,
  ///   changeType: 1, 1: 蓝牙修改后调用，2：网关调用
  ///   type 2-反锁开关、3-防撬通知开关、4-重置键开关、6-锁声音开关、7-开门方向、8-音量、10-Wifi锁省电模式开关、11-门未关报警时间设置
  ///   value: 设置值: 1-开启、2-关闭; 开门方向：1-左开、2-右开; 音量：1到5; 门未关报警时间：单位秒，0表示关闭，需要门磁
  /// }
  Future<Map> lockSetting(int lockId, data) async {
    Response response = await request.put(
      "device/lock/setting/$lockId",
      queryParameters: data,
    );
    return response.data ?? {};
  }

  /// 查询锁设置项
  Future<Map> getLockSetting(int lockId, type) async {
    Response response = await request.get(
      "device/lock/setting/$lockId/$type",
    );
    return response.data ?? {};
  }

  /// 标记门锁为坏锁
  Future<Map> markLockBroken(int lockId, queryParameters) async {
    Response response = await request.post(
      "device/lock/mark-broken/$lockId",
      queryParameters: queryParameters
    );
    return response.data ?? {};
  }

  /// 恢复坏锁为正常
  Future<Map> restoreLock(int lockId) async {
    Response response = await request.post(
      "device/lock/restore-broken/$lockId",
    );
    return response.data ?? {};
  }

}

LockApi lockApi = LockApi();